from flask import jsonify

from plugin.dk.Image import Image
from plugin.utils.request import input
from app.docker.router import docker
from plugin.utils.thread import Thread
import uuid
import re
image = Image()


@docker.route('image/list')
def getlist():

    data = {}
    data['code'] = 200
    returns = []
    for value in image.getList():
        tmpData = value
        tmpData['Id'] = value['Id'][7:]
        returns.append(tmpData)
    data['data'] = returns
    data['messsage'] = 'success'
    return jsonify(data)

@docker.route('image/remove', methods=['POST', 'DELETE', 'GET'])
def remove():
    """
    删除镜像
    :return:json
    """
    data = {}
    inputs = input('id')
    result = None
    for value in inputs:
        result = image.remove(value)
    if result == None:
        data['code'] = 200
    else:
        data['code'] = 500
        data['message'] = result.explanation
    return jsonify(data), data['code']


@docker.route('image/pull')
def pull():
    from ..common.image import downloadImage
    """
    拉取一个镜像
    :return:
    """
    data = {}
    id = uuid.uuid4()
    download = Thread(downloadImage, id, input('os'))
    download.start()
    data['code'] = 200
    data['message'] = 'download'
    return jsonify(data), data['code']

